package com.pirestupppai.widgets.dialog;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pirestupppai.R;
import com.pirestupppai.adapter.PsListAdapter;
import com.pirestupppai.base.BaseDialog;
import com.pirestupppai.bean.MissionData;
import com.pirestupppai.common.Constant;
import com.pirestupppai.rxbus.RxBus;
import com.pirestupppai.ui.activity.DetailActivity;
import com.pirestupppai.ui.activity.ImageEditActivity;
import com.pirestupppai.ui.activity.SelectPictureActivity;
import com.pirestupppai.utils.DownloadUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Subscription;
import rx.functions.Action1;

import static com.pirestupppai.adapter.PsListAdapter.PS_TYPE_REPLY_NOW;
import static com.pirestupppai.rxbus.RxBus.RXBUS_DOWNLOAD_RESULT;

/**
 * 帮P列表操作
 * 上传删除详情
 *
 * @author: xiaoluo
 * @date: 2017-04-26 11:40
 */
public class PsListOptionDialog extends BaseDialog implements View.OnClickListener {

    @BindView(R.id.release_tv)
    TextView uploadTv;
    @BindView(R.id.delete_tv)
    TextView deleteTv;
    @BindView(R.id.ps_tv)
    TextView psTv;
    @BindView(R.id.detail_tv)
    TextView detailTv;
    @BindView(R.id.option_layout)
    LinearLayout optionLayout;
    @BindView(R.id.close_layout)
    LinearLayout closeLayout;

    private MissionData mMission;
    private int mType;
    private Subscription mSubscription;
    private List<String> mDownloadList = new ArrayList<>();
    private ProgressingDialog mDialog;
    private int mCount = 0;

    public PsListOptionDialog(Context context, MissionData missionData, int type) {
        super(context, R.style.CustomDialogStyle);
        mMission = missionData;
        mType = type;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.ps_tv:
                download();
                break;
            case R.id.detail_tv:
                intent.setClass(mContext, DetailActivity.class);
                intent.putExtra(Constant.IntentKey.ID, mMission.getAsk_post().getId());
                mContext.startActivity(intent);
                dismiss();
                break;
            case R.id.delete_tv:
                dismiss();
                break;
            case R.id.release_tv:
                intent.setClass(mContext, SelectPictureActivity.class);
                intent.putExtra(Constant.IntentKey.PS_TYPE, mType);
                intent.putExtra(Constant.IntentKey.RELEASE_TYPE, Constant.SELECT_TYPE_WORK);
                if (mType == PS_TYPE_REPLY_NOW) {
                    intent.putExtra(Constant.IntentKey.PARENT_ID, mMission.getAsk_post().getId());
                } else {
                    intent.putExtra(Constant.IntentKey.PARENT_ID, mMission.getId());
                }
                mContext.startActivity(intent);
                dismiss();
                break;
            case R.id.close_layout:
                dismiss();
                break;
        }
    }

    /**
     * 下载
     */
    private void download() {
        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.show();
        }
        optionLayout.setClickable(false);
        DownloadUtil.downloadImage(mDownloadList);
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (!mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
        }
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.dialog_ps_option;
    }

    @Override
    protected boolean atBottom() {
        return true;
    }

    @Override
    protected void initViews() {
        mDialog = new ProgressingDialog(mContext);
        if (mMission.getAsk_post() != null && mMission.getAsk_post().getMedias() != null
                && mMission.getAsk_post().getMedias().size() != 0) {
            mDownloadList.add(mMission.getAsk_post().getMedias().get(0).getPathname());
        }

        //注册观察者
        mSubscription = RxBus.getInstance()
                .toObservable(RXBUS_DOWNLOAD_RESULT, String.class)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        mCount++;
                        if (mCount == mDownloadList.size()) {
                            if (mDialog != null && mDialog.isShowing()) {
                                mDialog.dismiss();
                            }
                            if (TextUtils.isEmpty(s)) {
                                optionLayout.setClickable(true);
                            } else {
                                Intent intent = new Intent(mContext, ImageEditActivity.class);
                                intent.putExtra(Constant.IntentKey.PS_TYPE, PsListAdapter.PS_TYPE_REPLY_NOW);
                                intent.putExtra(Constant.IntentKey.SELECT_LIST, s);
                                intent.putExtra(Constant.IntentKey.PARENT_ID, mMission.getAsk_post().getId());
                                mContext.startActivity(intent);
                                dismiss();
                            }
                        }
                    }
                });
    }

    @Override
    protected void initListeners() {
        uploadTv.setOnClickListener(this);
        deleteTv.setOnClickListener(this);
        psTv.setOnClickListener(this);
        detailTv.setOnClickListener(this);
        closeLayout.setOnClickListener(this);
    }
}
